//
//  HelpViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/27.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "HelpViewController.h"
#import "HelpCell.h"

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface HelpViewController ()
{
    BOOL flags[100];
}
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSArray *arr;
@end

@implementation HelpViewController

//懒加载
- (NSArray *)array
{
    if(_array == nil)
    {
        _array = [NSArray arrayWithObjects:@[@"请先注册就医宝账号，成功登录后在"],@[@"请在就掌上医院“账号登录页”点击“忘记密码”凭邮箱号找号，如果无法找回，请拨打客服热线10086,在核实身份后将为您找回密码。"],@[@"取消预约需要在就诊前一天下午3点之前操作，选择“我的－挂号订单－取消订单”即可。"],@[@"网上预约成功之后，凭有效证件和预约成功短信到达医院门诊处，在人工服务台或取号机完成取号，即可就诊。已完成在线支付挂号费的，可直接前往所挂科室候诊。"],@[@"短信接收可能会有延迟。如果半小时内没有收到，请拨打客服热线10086,我们核实信息后将补发给您。"], nil];
    }
    return _array;
}

//懒加载
- (NSArray *)arr
{
    if(_arr == nil)
    {
        _arr = [NSArray arrayWithObjects:@"1.怎样预约挂号？",@"2.忘记密码了怎么办？",@"3.已完成预约后如何取消？",@"4.网上预约成功之后如何就诊",@"5.预约成功之后收不到短信怎么办？", nil];
    }
    return _arr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"帮助中心";
    [self.tableView registerClass:[HelpCell class] forCellReuseIdentifier:@"Cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //刚开始显示的时候不让cell的文字直接显示出来，直到点击分区的头视图后，显示cell
    if(flags[section] == NO)
        return 0;
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HelpCell *cell = [HelpCell cellWithTableView:tableView];
    cell.contentLabel.text = self.array[indexPath.section][indexPath.row];
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //设置文字居左
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //设置文字离边框距上下都为10
    button.contentEdgeInsets = UIEdgeInsetsMake(10,10, 0, 0);
    NSString *str = self.arr[section];
    button.tag = section;
    [button setTitle:str forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

//在button按钮的动作里面实现 刷新表
- (void)btnAction:(UIButton *)button
{
    NSInteger index = button.tag;
    //改变每行的点击状态。用于显示和隐藏cell
    flags[index] = !flags[index];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取每个Cell上显示的文字
    NSString *str = self.array[indexPath.section][indexPath.row];
    //通过文字获取文字所占的Size大小
    CGSize height = [self sizeWithText:str font:[UIFont systemFontOfSize:15] maxSize:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT)];
    return height.height + 21;
}

/**
 *  计算文字尺寸
 *
 *  @param text    需要计算尺寸的文字
 *  @param font    文字的字体
 *  @param maxSize 文字的最大尺寸
 */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
